package cn.edu.neu.springbootdemo.service

import org.apache.commons.io.FileUtils
import org.apache.ibatis.jdbc.Null
import org.springframework.web.multipart.MultipartFile
import java.io.File
import java.io.IOException

fun addPic(file: MultipartFile?,name: String?): String{
    val path = System.getProperty("user.dir")+"\\src\\main\\resources\\static\\images\\"
    val filePath = File(path)
    if (!filePath.exists()){
        filePath.mkdirs()
    }
    if (file?.isEmpty!!){
        return " "
    }
    val suf = file.originalFilename.substring(file.originalFilename.indexOf(".")+1)
    val fileNameA = "$name.$suf"
    return try {
        FileUtils.writeByteArrayToFile(File(path+fileNameA),file.bytes)
        "../images/$fileNameA"
    }catch (e: IOException){
        e.printStackTrace()
        " "
    }
}